package tv.youxing.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.adapter.AlbumVideoAdapter;
import tv.youxing.android.custom.CustomListView;
import tv.youxing.android.entity.AlbumInfoModel;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.ui.SubscribeDeteilsActivity;
import tv.youxing.android.utils.L;
import tv.youxing.android.utils.UtilTools;

/**
 * Created by apple on 2017/4/22.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .fragment
 * 文件名：   ProgramFragment
 * 创建者：   叶克武
 * 创建时间： 2017/4/22 上午9:54
 * 描述：    TODO
 */
public class ProgramFragment extends Fragment {

    private TextView tvEpisode;//节目数量
    private TextView tvSort;//排序
    private CustomListView videoListView;
    private List<AlbumInfoModel.ResultBean.MultimediaBean> albumVideoModelList = new ArrayList<>();


    AlbumInfoModel allData;


    /**
     * true 升序
     * false 降序
     */
    boolean is_soft=true;
    private AlbumVideoAdapter mAlbumVideoAdapter;


    public ProgramFragment(AlbumInfoModel allData) {
        this.allData = allData;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_program, null);
        findView(view);
//        EventBus.getDefault().register(getActivity());
        return view;
    }

    private void findView(View view) {
        albumVideoModelList = allData.getResult().getMultimedia_list();

        tvEpisode = (TextView) view.findViewById(R.id.tvEpisode);
        videoListView = (CustomListView) view.findViewById(R.id.videoListView);
        tvSort = view.findViewById(R.id.sort);


        tvEpisode.setText("共 " + allData.getResult().getMultimedia_count() + " 集");

        tvSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setListSort();
            }
        });
        setListSort();

        videoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlbumInfoModel.ResultBean.MultimediaBean m = albumVideoModelList.get(i);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count()+"");
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setIs_collectied(m.getIs_collectied());
                model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                L.d("专辑名称是:" + m.getAlbum().getName());

                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(getActivity(), SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });
    }




//    videoListView = (CustomListView) view.findViewById(R.id.videoListView);
//        videoListView.setAdapter(new AlbumVideoAdapter(getActivity(), albumVideoModelList));
//        videoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                L.i("album videoListView.setOnItemClickListener........");
//            }
//        });

//    }


    /**
     * 对list排序 根据日期
     */
    void setListSort() {

        // sort = 1 after 降序 上小下大
        // sort = 2 before 升序 上大下小 默认



        if (is_soft) {
            Collections.sort(albumVideoModelList, new Comparator<AlbumInfoModel.ResultBean.MultimediaBean>() {

                /**
                 *
                 * @param lhs
                 * @param rhs
                 * @return an integer < 0 if lhs is less than rhs, 0 if they are
                 *         equal, and > 0 if lhs is greater than rhs,比较数据大小时,这里比的是时间
                 */
                @Override
                public int compare(AlbumInfoModel.ResultBean.MultimediaBean lhs, AlbumInfoModel.ResultBean.MultimediaBean rhs) {
                    Date date1 = UtilTools.stringToDate(lhs.getUpdated_at());
                    Date date2 = UtilTools.stringToDate(rhs.getUpdated_at());
                    // 对日期字段进行升序，如果欲降序可采用after方法

                    //after 降序 上小下大
                    //before 升序 上大下小

                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }



            });


            videoListView.setAdapter(new AlbumVideoAdapter(getActivity(), albumVideoModelList));
        } else{
            Collections.sort(albumVideoModelList, new Comparator<AlbumInfoModel.ResultBean.MultimediaBean>() {

                /**
                 *
                 * @param lhs
                 * @param rhs
                 * @return an integer < 0 if lhs is less than rhs, 0 if they are
                 *         equal, and > 0 if lhs is greater than rhs,比较数据大小时,这里比的是时间
                 */
                @Override
                public int compare(AlbumInfoModel.ResultBean.MultimediaBean lhs, AlbumInfoModel.ResultBean.MultimediaBean rhs) {
                    Date date1 = UtilTools.stringToDate(lhs.getUpdated_at());
                    Date date2 = UtilTools.stringToDate(rhs.getUpdated_at());
                    // 对日期字段进行升序，如果欲降序可采用after方法

                    //after 降序 上小下大
                    //before 升序 上大下小

                    if (date1.after(date2)) {
                        return 1;
                    }
                    return -1;
                }



            });
            mAlbumVideoAdapter = new AlbumVideoAdapter(getActivity(), albumVideoModelList);
            videoListView.setAdapter(mAlbumVideoAdapter);
        }

        is_soft = !is_soft;



    }




    @Override
    public void onResume() {
        super.onResume();

        if (mAlbumVideoAdapter != null) {
            mAlbumVideoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(getActivity());
    }
}
